import $ from '../fn/data_type'
import $wx from '../wx'
const Index = {
    regex: {
        phone: {
            reg: /^1[3-9]\d{9}$/,
            msg: '手机号格式不正确'
        },
        password: {
            reg: /^(?=.*[a-z])(?=.*\d)[^]{6,18}$/,
            msg: '密码必须包含数字、字母, 且不少于6位, 不多于 18 位'
        },
        idcard: {
            reg: /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
            msg: '身份证格式不正确'
        },
        bank_card: {
            reg: /^[0-9]{9,18}$/,
            msg: '银行卡格式不正确'
        },
        email: {
            reg: /^([A-Za-z0-9_\-.])+@([A-Za-z0-9_\-.])+\.([A-Za-z]{2,4})$/,
            msg: '邮箱格式不正确'
        },
        max: (value, len)=>({
            test: () => value < len,
            msg: `最大值为 ${len}`
        }),
        min: (value, len)=>({
            test: () => value > len,
            msg: `最大值为 ${len}`
        }),
        maxLen: (value, len)=>({
            test: () => value.length < len,
            msg: `最长为 ${len} 个字符`
        }),
        minLen: (value, len)=>({
            test: () => value.length > len,
            msg: `最小为 ${len} 个字符`
        }),
        len: (value, len)=>({
            test: () => len === value.length,
            msg: `长度为 ${len}`
        })
    },
    test( name, value, len, label ){
        const d = this.regex?.[name]
        if( !d ) {
            return { value: null, msg: `验证模式 ${name} 不存在` }
        }
        if( $.isFunction(d) ){
            const { test, msg } = d(value, len)
            return { value: test(), msg:label ? label + msg : msg }
        }else if( $.isObject(d) ){   // 对象是正则表达式
            const { reg, msg } = d
            return { value: reg.test(value), msg:label ? label + msg : msg  }
        }
     }
}

export default Index
